// pages/play/play.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    postid:"",
    playItem:{},
    playLink:"",
    playTime:"",
    relateList:[],
    commentList:[],
    isCollected:false,
    isLiked:false
  },

  // 处理时间
  getPlayTime(time){
    let m = Math.floor(time/60);
    let s= time%60;
    m=m<10?"0"+m:m;
    s=s<10?"0"+s:s;
    let str= `${m}'${s}'`;
    this.setData({
      playTime:str
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let postid=options.id;
    this.setData({
      postid:postid
    })
    this.getPlayItem();
    this.queryColleedByUser();
    this.queryLikedByUser();
  },
  getPlayItem(){
    wx.showLoading({
      title: '正在加载···',
    })
    wx.request({
      url: 'https://api.kele8.cn/agent?url=https://app.vmovier.com/apiv3/post/view?postid='+this.data.postid,
      success:res=>{
        wx.hideLoading();
        let data = res.data.data
        // console.log(data);
        let {source_link}= data.content_video[0];
        let {duration,relate_video}= data;
        this.setData({
          playItem:data,
          playLink:source_link,
          relateList:relate_video,
          commentList:data.comment.list
        })
        this.getPlayTime(duration)
      }
    })
  },
  // 收藏
  collect(){
    if (app.globalData.userInfo) {
      let db = wx.cloud.database(); //获取云数据库实例对象
      let userCollect = db.collection("userCollect"); //获取云数据库里面userCollect集合实例
      userCollect.where({
        postid:this.data.playItem.postid
      }).get().then(res=>{
        if (res.data.length>0) {
          wx.showToast({
            icon:"error",
            title: '您已收藏过了',
          })
        }else{
          this.data.playItem.colTime = new Date().getTime();
          this.data.playItem.isCollected = true;
          userCollect.add({
            data:this.data.playItem
          }).then(res=>{
            wx.showToast({
              title: '收藏成功！',
            })
            this.setData({
              isCollected:true
            })
          }).catch(err=>{
            wx.showToast({
              icon:"error",
              title: '收藏失败！',
            })
          })
        }
      })
    }else{
      // 授权登录
      wx.getUserProfile({
        desc: '用于登录',
        success:res=>{
          app.globalData.userInfo=res;
          this.queryColleedByUser();
          // 保存用户的openId
          if (!app.globalData.onpeId) {
            wx.cloud.callFunction({
              name:"changku"
            }).then(res=>{
              app.globalData.openId = res.result.openid;
            })
          }
        },
        fail:err=>{
          console.log("授权失败",err);
        }
      })
    }
  },

  // 取消收藏
  cancleCollect(){
    if (app.globalData.userInfo) {
      let userCollect = wx.cloud.database().collection("userCollect");
      userCollect.where({
        postid:this.data.postid
      }).remove().then(res=>{
        if (res.stats.removed>0) {
          wx.showToast({
            title: '取消成功',
          })
          this.setData({
            isCollected:false
          })
        }
      }).catch(err=>{
        wx.showToast({
          icon:"error",
          title: '取消失败！',
        })
      })
    }
  },
  // 查询是否已经被用户收藏
  queryColleedByUser(){
    if (app.globalData.userInfo) {
      let userCollect = wx.cloud.database().collection("userCollect");
      userCollect.where({
        postid:this.data.postid,
        isCollected:true
      }).get().then(res=>{
        if (res.data.length>0) {
          this.setData({
            isCollected:true
          })
        }
      })
    }
  },
  // 查询是否已经被用户点赞
  queryLikedByUser(){
    if (app.globalData.userInfo) {
      let userLike = wx.cloud.database().collection("userLike");
      userLike.where({
        postid:this.data.postid,
        isLiked:true
      }).get().then(res=>{
        if (res.data.length>0) {
          this.setData({
            isLiked:true
          })
        }
      })
    }
  },

  // 点赞
  like(){
    if (app.globalData.userInfo) {
      let db = wx.cloud.database(); //获取云数据库实例对象
      let userLike = db.collection("userLike"); //获取云数据库里面userLike集合实例
      userLike.where({
        postid:this.data.playItem.postid
      }).get().then(res=>{
        if (res.data.length>0) {
          wx.showToast({
            icon:"error",
            title: '您已点赞过了',
          })
        }else{
          this.data.playItem.likeTime = new Date().getTime();
          this.data.playItem.isLiked = true;
          userLike.add({
            data:this.data.playItem
          }).then(res=>{
            wx.showToast({
              title: '点赞成功！',
            })
            this.setData({
              isLiked:true
            })
          }).catch(err=>{
            wx.showToast({
              icon:"error",
              title: '点赞失败！',
            })
          })
        }
      })
    }else{
      // 授权登录
      wx.getUserProfile({
        desc: '用于登录',
        success:res=>{
          app.globalData.userInfo=res;
          this.queryColleedByUser();
          // 保存用户的openId
          if (!app.globalData.onpeId) {
            wx.cloud.callFunction({
              name:"changku"
            }).then(res=>{
              app.globalData.openId = res.result.openid;
            })
          }
        },
        fail:err=>{
          console.log("授权失败",err);
        }
      })
    }
  },
  // 取消点赞
  cancelLike(){
    if (app.globalData.userInfo) {
      let userLike = wx.cloud.database().collection("userLike");
      userLike.where({
        postid:this.data.postid
      }).remove().then(res=>{
        if (res.stats.removed>0) {
          wx.showToast({
            title: '取消成功',
          })
          this.setData({
            isLiked:false
          })
        }
      }).catch(err=>{
        wx.showToast({
          icon:"error",
          title: '取消失败！',
        })
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  // 分享给朋友
  onShareAppMessage: function () {
    return{
      title:this.data.playItem.title,
      imageUrl:this.data.playItem.image
    }
  },

  // 分享到朋友圈
  onShareTimeline:function () {
    return{
      title:this.data.playItem.title,
      imageUrl:this.data.playItem.image
    }
  }

})